package com.bobochang.warehouse.controller;

import com.bobochang.warehouse.annotation.BusLog;
import com.bobochang.warehouse.constants.WarehouseConstants;
import com.bobochang.warehouse.entity.ConstructionType;
import com.bobochang.warehouse.entity.LocationConstruction;
import com.bobochang.warehouse.entity.Result;
import com.bobochang.warehouse.page.Page;
import com.bobochang.warehouse.service.ConstructionTypeService;
import com.bobochang.warehouse.service.LocationConstructionService;
import com.bobochang.warehouse.service.impl.ConstructionTypeServiceImpl;
import com.bobochang.warehouse.utils.TokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RequestMapping("/constructionType")
@RestController
@Transactional
@BusLog(name = "施工类型管理")
@Slf4j
public class ConstructionTypeController {

    @Resource
    private LocationConstructionService locationService;
    @Resource
    private ConstructionTypeService constructionTypeService;
    @Autowired
    private TokenUtils tokenUtils;

    /**
     * 查询所有施工类型
     *
     * @return
     */
    @GetMapping("/constructionType-list")
    public Result constructionTypeList() {
        return Result.ok(constructionTypeService.findAllList());
    }

    /**
     * 根据项目id查
     */
    @GetMapping("/constructionType-list/{id}")
    public Result constructionTypeListById(@PathVariable Integer id) {
        return Result.ok(constructionTypeService.findAllListById(id));
    }

    /**
     * 分页查询施工类型
     *
     * @param page
     * @param
     * @return
     */
    @RequestMapping("/constructionType-page-list")
    public Result constructionTypePageList(Page page, ConstructionType constructionType) {
        // 分页查询施工类型
        Page resultPage = constructionTypeService.queryLocationPage(page, constructionType);
        return Result.ok(resultPage);
    }

    /**
     * 添加施工类型
     */
    @RequestMapping("/constructionType-add")
    @BusLog(descrip = "添加施工类型")
    public Result addConstructionType(@RequestBody ConstructionType constructionType,
                              @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token) {
        // 添加施工位置
        boolean isSave = constructionTypeService.save(constructionType);
        if (isSave) {
            return Result.ok("添加成功");
        } else {
            return Result.err(Result.CODE_ERR_BUSINESS, "添加失败");
        }
    }

    /**
     * 删除施工类型
     * @param constructionTypeId
     * @return
     */
    @RequestMapping("/constructionType-delete/{constructionTypeId}")
    @BusLog(descrip = "删除施工类型")
    public Result deleteConstructionType(@PathVariable Integer constructionTypeId) {
        boolean isDelete = constructionTypeService.removeById(constructionTypeId);
        if (isDelete) {
            return Result.ok("删除成功");
        } else {
            return Result.err(Result.CODE_ERR_BUSINESS, "删除失败");
        }
    }

    /**
     * 修改施工类型
     * @param constructionType
     * @param token
     * @return
     */
    @RequestMapping("/constructionType-update")
    @BusLog(descrip = "修改施工类型")
    public Result updateConstructionType(@RequestBody ConstructionType constructionType,
                                 @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token) {
        // 修改施工位置
        boolean isUpdate = constructionTypeService.updateById(constructionType);
        if (isUpdate) {
            return Result.ok("修改成功");
        } else {
            return Result.err(Result.CODE_ERR_BUSINESS, "修改失败");
        }
    }



}
